import { defineStore } from 'pinia'
import type { LoginReq } from '@/api/user/types.ts'
import { loginHttp } from '@/api/user'
import type { Login } from '@/store/types/login.ts'

const useLoginStore = defineStore('Login', {
  state: (): Login => ({
    token: localStorage.getItem('token') || ''
  }),
  actions: {
    async login(form: LoginReq) {
      console.log('this:', this, form)
      const res = await loginHttp(form)
      console.log('登录接口res：', res)
      if (res.code === 200) {
        this.token = res.data.token
        localStorage.setItem('token', (this.token as string))
      } else {
        return Promise.reject(new Error(res.data.message))
      }
    }
  },
  getters: {}
})

export default useLoginStore
